package com.foxconn.idf_demo;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
//import com.sdsmdg.tastytoast.TastyToast;


import androidx.appcompat.app.AppCompatActivity;

import com.foxconn.idf_demo.databinding.ActivityMainBinding;
import com.sdsmdg.tastytoast.TastyToast;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    @BindView(R.id.button13)
      Button btn1;

    @BindView(R.id.button14)
      Button btn2;

    @BindView(R.id.button15)
     Button btn3;

    @BindView(R.id.button16)
     Button btn4;

    @BindView(R.id.button17)
     Button btn5;
    @BindView(R.id.button18)
      Button btn6;

    @BindView(R.id.button19)
      Button btn7;
    @BindView(R.id.button20)
      Button btn8;
    @BindView(R.id.button21)
      Button btn9;
    @BindView(R.id.button22)
      Button btn10;

    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ButterKnife.bind(this); //初始化控件


//        SystemControlManager systemControlManager = (SystemControlManager) getSystemService("system_control");
        //btn 1-9 输入条件相同

        initBtns();

    }
    private  int led_flag =0;
    private  int led_flag2 =0;
    private  int led_flag3 =0;
    private  int Light_value =0;
    private  int screenBrightness_value =0;
    String serialNo="";
    public void initBtns() {
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (led_flag == 0 ){
                    flashLedControl(1, true);
                    led_flag = 1;
                }else{
                    flashLedControl(1, false);
                    led_flag = 0;
                }

            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (led_flag2 == 0 ){
                    flashLedControl(2, true);
                    led_flag2 = 1;
                }else{
                    flashLedControl(2, false);
                    led_flag2 = 0;
                }

            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (led_flag3 == 0 ){
                    flashLedControl(3, true);
                    led_flag3 = 1;
                }else{
                    flashLedControl(3, false);
                    led_flag3 = 0;
                }

            }
        });
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Light_value+=25;
                fillLightControl(Light_value);
                if (Light_value >=255  ){
                    Light_value=0;
                }

            }
        });
        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                screenBrightness_value+=25;
                screenBrightnessControl(screenBrightness_value);
                if (screenBrightness_value >=255  ){
                    screenBrightness_value=0;
                }

            }
        });
        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                serialNo= getSerialNo();
                mHandler.sendEmptyMessage(6);


            }
        });
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (led_flag == 0 ){
                    flashLedControl(1, true);
                    led_flag = 1;
                }else{
                    flashLedControl(1, false);
                    led_flag = 0;
                }

            }
        });
        btn9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rebootSystem();

            }
        });
        btn10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shutdownSystem();


            }
        });


    }


    @SuppressLint("WrongConstant")
    public String flashLedControl(int type, boolean status) {
        try {
            //return (String) ReflectionUtil.invoke(Class.forName("android.os.SystemProperties"),"get",key);
            Class cls = Class.forName("android.os.SystemControlManager");
            Method setMethod = cls.getDeclaredMethod("flashLedControl",int.class,boolean.class);
            setMethod.invoke(this.getSystemService("system_control"), type,status);
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
    @SuppressLint("WrongConstant")
    public String dispatchView(int type, boolean status) {
        try {
            //return (String) ReflectionUtil.invoke(Class.forName("android.os.SystemProperties"),"get",key);
            Class cls = Class.forName("android.os.SystemControlManager");
            Method setMethod = cls.getDeclaredMethod("dispatchView",int.class,boolean.class);
            setMethod.invoke(this.getSystemService("system_control"), type,status);
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
    @SuppressLint("WrongConstant")
    public String fillLightControl(int value) {
        try {
            //return (String) ReflectionUtil.invoke(Class.forName("android.os.SystemProperties"),"get",key);
            Class cls = Class.forName("android.os.SystemControlManager");
            Method setMethod = cls.getDeclaredMethod("fillLightControl",int.class);
            setMethod.invoke(this.getSystemService("system_control"), value);
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
    @SuppressLint("WrongConstant")
    public String screenBrightnessControl(int value) {
        try {
            //return (String) ReflectionUtil.invoke(Class.forName("android.os.SystemProperties"),"get",key);
            Class cls = Class.forName("android.os.SystemControlManager");
            Method setMethod = cls.getDeclaredMethod("screenBrightnessControl",int.class);
            setMethod.invoke(this.getSystemService("system_control"), value);
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
    @SuppressLint("WrongConstant")
    public String rebootSystem() {
        try {
            //return (String) ReflectionUtil.invoke(Class.forName("android.os.SystemProperties"),"get",key);
            Class cls = Class.forName("android.os.SystemControlManager");
            Method setMethod = cls.getDeclaredMethod("screenBrightnessControl");
            setMethod.invoke(this.getSystemService("system_control"));
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
    @SuppressLint("WrongConstant")
    public String shutdownSystem() {
        try {
            //return (String) ReflectionUtil.invoke(Class.forName("android.os.SystemProperties"),"get",key);
            Class cls = Class.forName("android.os.SystemControlManager");
            Method setMethod = cls.getDeclaredMethod("shutdownSystem");
            setMethod.invoke(this.getSystemService("system_control"));
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
    @SuppressLint("WrongConstant")
    public String getSerialNo() {
        String ret = null;
        try {
            //return (String) ReflectionUtil.invoke(Class.forName("android.os.SystemProperties"),"get",key);
            Class cls = Class.forName("android.os.SystemControlManager");
            Method setMethod = cls.getDeclaredMethod("getSerialNo");
            ret =(String )setMethod.invoke(this.getSystemService("system_control"));
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return ret;
    }

    public Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case -1:

                    break;
                case 6:
                    TastyToast.makeText(MainActivity.this, serialNo, TastyToast.LENGTH_SHORT, TastyToast.INFO);
                    break;
            }
            return false;
        }
    });

}